package com.youlai.boot.system.service.impl;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youlai.boot.system.mapper.SysDianciListMapper;
import com.youlai.boot.system.service.SysDianciListService;
import com.youlai.boot.system.model.entity.SysDianciList;
import com.youlai.boot.system.model.form.SysDianciListForm;
import com.youlai.boot.system.model.query.SysDianciListQuery;
import com.youlai.boot.system.model.vo.SysDianciListVO;
import com.youlai.boot.system.converter.SysDianciListConverter;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;

/**
 * 电池列表服务实现类
 *
 * @author liwenbing
 * @since 2025-04-09 12:31
 */
@Service
@RequiredArgsConstructor
public class SysDianciListServiceImpl extends ServiceImpl<SysDianciListMapper, SysDianciList> implements SysDianciListService {

    private final SysDianciListConverter sysDianciListConverter;

    /**
    * 获取电池列表分页列表
    *
    * @param queryParams 查询参数
    * @return {@link IPage<SysDianciListVO>} 电池列表分页列表
    */
    @Override
    public IPage<SysDianciListVO> getSysDianciListPage(SysDianciListQuery queryParams) {
        Page<SysDianciListVO> pageVO = this.baseMapper.getSysDianciListPage(
                new Page<>(queryParams.getPageNum(), queryParams.getPageSize()),
                queryParams
        );
        return pageVO;
    }
    
    /**
     * 获取电池列表表单数据
     *
     * @param id 电池列表ID
     * @return
     */
    @Override
    public SysDianciListForm getSysDianciListFormData(Long id) {
        SysDianciList entity = this.getById(id);
        return sysDianciListConverter.toForm(entity);
    }
    
    /**
     * 新增电池列表
     *
     * @param formData 电池列表表单对象
     * @return
     */
    @Override
    public boolean saveSysDianciList(SysDianciListForm formData) {
        SysDianciList entity = sysDianciListConverter.toEntity(formData);
        return this.save(entity);
    }
    
    /**
     * 更新电池列表
     *
     * @param id   电池列表ID
     * @param formData 电池列表表单对象
     * @return
     */
    @Override
    public boolean updateSysDianciList(Long id,SysDianciListForm formData) {
        SysDianciList entity = sysDianciListConverter.toEntity(formData);
        return this.updateById(entity);
    }
    
    /**
     * 删除电池列表
     *
     * @param ids 电池列表ID，多个以英文逗号(,)分割
     * @return
     */
    @Override
    public boolean deleteSysDianciLists(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除的电池列表数据为空");
        // 逻辑删除
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .toList();
        return this.removeByIds(idList);
    }

}
